package middlewares

import (
	"gitee.com/go-micro-services/api/utils"
	"gitee.com/go-micro-services/common"
	"github.com/gin-gonic/gin"
)

// 拦截器
func Trace() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		tracer, closer, err := common.NewTracer(utils.AppName, utils.TracerAddr)
		if err != nil {
			panic(err)
		}
		defer closer.Close()

		// 最开始的span，以url开始
		startSpan := tracer.StartSpan(ctx.Request.URL.Path)

		defer startSpan.Finish()

		ctx.Set("tracer", tracer)
		ctx.Set("parentSpan", startSpan)
		ctx.Next()
	}
}
